<template>
  <div class="good">
    <warp></warp>
    <div class="title">
      <div>
        <ul>
          <li>服饰内衣</li>
          <li>> </li>
          <li>女装 </li>
          <li>> </li>
          <li>女生衬衫</li>
          <li>> </li>
          <li>女士长袖衬衫</li>
        </ul>
        <div style="flex: 1;"></div>
      </div>
      <div class="head">
        <div style="display: flex;"><!--左边-->
          <img src="../assets/dianpu.png" alt="">
          <div>
            <p>京喜自营官方店</p>
            <input type="button" value="已关注">
            <input type="button" value="联系客服">
          </div>
        </div>
        <div><!--右边-->
          <div style="border: 1px solid red ;border-radius: 3px; ">
            <div class="ssk"><!--搜索框-->
              <input type="text">
              <button>搜本店</button>
              <button>搜全网</button>
            </div><!--搜索框-->
          </div>
          <div><!--搜索框下方推荐-->
            <ul>
              <li>衬衫女2025春季</li>
              <li>衬衫女轻奢高级感</li>
              <li>衬衫女</li>
              <li>衬衫格纹</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="scrollable-side"><!--左边的滑动部分-->
        <div class="img-container"><!--图片列表-->
          <div class="thumbnails">
            <!-- 缩略图列表 -->
            <div v-for="(image, index) in list" :key="index" @click="changeImage(index)">
              <img class="thumbnail" :src="image" alt=""></img>
            </div>
          </div>
          <div class="main-image">
            <!-- 大图展示 -->
            <img :src="list[imgindex]" alt="" class="main-img">
          </div>
        </div>
        <div class="pinglunbiaoqian">
          <h2>买家评论</h2>
          <ul>
            <li>颜色漂亮</li>
            <li>质感超棒</li>
            <li>面料适中</li>
            <li>颜色无色差</li>
          </ul>
        </div>
        <div class="评论" v-for="(comment,index) in comments" :key="index">
          <comment :comment="comment"></comment>
        </div>
        <div class="recommendation"><!--介绍-->
          商品详情：
          <div class="introduce" v-for="(img,index) in introduceImg" :key="index">
            <img class="introduceImg" :src="img" alt="">
          </div>
        </div><!--介绍-->
      </div>

      <div class="fixed-side"><!--右边的固定部分-->
        <div class="promo-banner">
          <span class="min_title">心动购物季</span>
          <span class="subtitle">好用又便宜</span>
        </div>
        <!--商品信息-->
        <div class="main">
          <span class="good_title">
            {{ product.title }}</span>
          <p class="price">￥{{ product.price }}
            <text class="标签">到手价</text>
            <span class="price_after">￥{{ product.price+20}}</span>
          </p>
          <div class="color-box">
            <p style="padding-right: 50px">送至</p>
            <select style="width: 100px;" v-model="product.address">
              <option value="北院">北院</option>
              <option value="南院">南院</option>
              <option value="东院">东院</option>
            </select>
          </div>
          <div class="color-box">
            <div class="color-title">颜色</div>
            <ul>
              <li v-for="(color, index) in colorList" :key="index"
                  :class="{ 'active-color': colorindex === index }"
                  @click="colorindex = index">
                <img class="colorImg" :src="color.img" alt="">
                <div>{{ color.name }}</div>
              </li>
            </ul>
          </div><!--颜色-->
          <div class="color-box">
            <div class="chima_title">尺码</div>
            <ul>
              <li v-for="(chima, index) in chimaList" :key="index"
                  :class="{ 'active-size': chimaindex === index }"
                  @click="chimaindex = index">
                <div>{{ chima.name }}</div>
              </li>
            </ul>
          </div><!--尺码-->

          <div class="actions">
            <el-input-number style="margin: 20px;" v-model="num" @change="handleChange" :min="1" :max="10" label="数量"></el-input-number><!--数量-->
            <button @click="joincart">加入购物车</button>
            <button>立即购买</button>
          </div>
        </div>


      </div>
    </div>
    <div>
      <End></End>
      {{product}}
    </div>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import { useRoute, useRouter } from "vue-router";
import Warp from "@/components/warp";
import Comment from "@/components/comment";
import End from "@/components/end";
const route = useRoute();
const router = useRouter();
const num = ref(1);
const product = ref({
  title: "",
  price: 0,
  img: "",
  address: "北院",
  size: '',
  color: '',
  number: 1,// 初始值设为1
});

// 在组件挂载后获取路由参数
onMounted(() => {
  if (route.query.product) {
    try {
      // 解析路由参数中的商品数据
      const productData = JSON.parse(route.query.product);
      // 使用解析后的数据更新响应式对象
      product.value = {
        ...product.value, // 保留默认值
        ...productData    // 用路由数据覆盖
      };
    } catch (error) {
      console.error("解析商品数据失败:", error);
    }
  }
});

const joincart = () => {
  // 更新商品信息
  product.value.number = num.value;
  product.value.size = chimaList.value[chimaindex.value].name;
  product.value.color = colorList.value[colorindex.value].name;
  product.value.img = list.value[imgindex.value]; // 修正：使用imgindex.value

  console.log("加入购物车的商品:", product.value);

  // 创建一个纯JavaScript对象，避免响应式数据的循环引用问题
  const plainProduct = { ...product.value };

  // 导航到购物车页面
  router.push({
    path: "/cart.vue",
    query: { product: JSON.stringify(plainProduct) }
  });
};

// 图片轮播
const list = ref([
  require("../assets/tuoluo.png"),
  require("../assets/lunbotu.png"),
  require("../assets/tuoluo.png"),
  require("../assets/lunbotu.png"),
  require("../assets/lunbotu.png")
]);
const imgindex = ref(0);

const changeImage = (index) => {
  imgindex.value = index;
};


// 评论数据
const comments = ref([
  {touxiang:require("../assets/touxiang.png"),name:"我是评论家",content:"我好喜欢这个商品",imgs:[require("../assets/punglun.png"),require("../assets/punglun.png"),require("../assets/punglun.png")]},
  {touxiang:require("../assets/touxiang.png"),name:"我是评论家",content:"我好喜欢这个商品",imgs:[require("../assets/punglun.png"),require("../assets/punglun.png"),require("../assets/punglun.png")]},
  {touxiang:require("../assets/touxiang.png"),name:"我是评论家",content:"我好喜欢这个商品",imgs:[require("../assets/punglun.png"),require("../assets/punglun.png"),require("../assets/punglun.png")]},
  {touxiang:require("../assets/touxiang.png"),name:"我是评论家",content:"我好喜欢这个商品",imgs:[require("../assets/punglun.png"),require("../assets/punglun.png"),require("../assets/punglun.png")]}
]);

// 商品详情图片
const introduceImg = ref([
  require("../assets/writer.png"),
  require("../assets/yellow.png"),
  require("../assets/writer.png"),
  require("../assets/yellow.png"),
  require("../assets/writer.png"),
  require("../assets/yellow.png"),
]);

// 颜色选择
const colorindex = ref(0);
const colorList = ref([
  {name:"棕色", img:require("../assets/yellow.png")},
  {name:"白色", img:require("../assets/writer.png")},
  {name:"黄色", img:require("../assets/yellow.png")}
]);

// 尺码选择
const chimaindex = ref(0);
const chimaList = ref([
  {name:"XS"},
  {name:"S"},
  {name:"M"},
  {name:"L"},
  {name:"XL"},
]);

</script>

<style scoped>
.title {
  width: 80%;
}
.评论{
  margin-top: 10px;
}
.head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
}
.good {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.container {
  width: 80%;
  display: flex;
  height: 100vh; /* 占满整个视口高度 */
}

.fixed-side {
  display: flex;
  flex-direction: column; /* 垂直排列 */
  width: 50%; /* 固定部分占一半宽度 */
  box-sizing: border-box;
  overflow: hidden; /* 禁止滚动 */
  background:#ff4b2b;
  border-radius: 10px;
}
.scrollable-side {
  width: 50%; /* 可滚动部分占另一半宽度 */
  padding: 5px;
  box-sizing: border-box;
  overflow-y: auto; /* 垂直方向可滚动 */
  background-color: white;
  padding-right: 20px;
}
.title ul {
  display: flex;
  list-style: none;
  padding: 8px 0px;
  margin: 0;
  flex-wrap: wrap;
}
.title li {
  margin-right: 10px;
  font-size: 12px;
  color: #666;
  cursor: pointer;
}
.title li:hover {
  color: #ff0000;
}
.ssk {
  border: 1px solid white;
  display: flex;
  align-items: center;
}

.ssk input {
  border-radius: 5px;
  border: 1px solid white;
  padding: 5px 10px;
}

.ssk button {
  border: none;
  padding: 5px 10px;
}

.ssk button:first-of-type {
  border-radius: 5px;
  background-color: pink;
}

.ssk button:last-of-type {
  border-radius: 5px;
  background-color: red;
  color: white;
}

/* 图片容器样式 */
.img-container {
  display: flex;
  gap: 10px;
  width: 100%;
  height: 720px;
}

/* 缩略图样式 */
.thumbnails {
  display: flex;
  height:700px;
  overflow-y: scroll;
  flex-direction: column;
  gap: 10px;
  width: 20%;

}

.thumbnail {
  width: 100%;
}

/*大背景*/
.main-image {
  width: 80%;
  height:700px;
  background-color: #fff;
  overflow: hidden;

}
.main-img {
  object-fit: contain;
  transition: transform 0.3s;
  height:700px;
;
}
/*价格*/
.price {
  display: flex;
  align-items: center;
  color: red;
  font-size: 24px;
}

.actions {
  position: relative;

  margin-buttom: 30px;
}

.actions button {
  padding: 10px 20px;
  margin-right: 10px;
  background:#ff4b2b;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 16px;
  border-radius: 5px;
}

.actions button:hover {
  background:red;
}

.main{
  padding: 20px;
  border-radius:  10px;
  flex: 1;
  heitht: 100%;
  width: 100%;
  background: #fff;
}
/*最上边的*/
.promo-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 24px;
  background:#ff4b2b;
  cursor: pointer;
  min-width: 280px;
}

.min_title {
  color: white;
  font-size: 20px;
  font-weight: 600;
}
.subtitle {
  color: white;
  font-size: 16px;
}
.good_title{
  font-size: 25px;
  margin: 10px;
}

.pinglunbiaoqian ul{
  display: flex;
  justify-items: center;
  align-items: center;
  margin: 10px;
}
.pinglunbiaoqian li{
  list-style: none;
  margin-right: 10px;
  padding: 5px 10px;
  background-color: #fff4e8;
  border-radius: 5px;
}
.introduceImg{
  width: 100%;
}

.标签{
  font-size: 12px;
  background-color: red;
  color: white;
  padding: 5px 10px;
  margin-left: 10px;
}
.price_after{
  color:gray;
  font-size: 20px;
  margin-left: 10px;
  font-weight: 300;
  text-decoration: line-through;
  margin-top: 10px;
  margin-bottom: 10px;
}

.color-box{
  display: flex;
  align-items: center;
  margin: 10px;
}
.main ul{
  display: flex;
  align-items: center;
}
.main li{
  display: flex;
  align-items: center;
  margin-right: 10px;
  padding: 5px;
  background-color: #f7f8fc;
}
.main li:hover{
  background-color: #ffebf1;
  border: 1px  solid #ff4b2b;
}
.colorImg{
  width: 20px;
  height: 20px;
}

.active-color {
  color: #ff4b2b;
  border: 1px solid #ff4b2b;
}
.active-size {
  color: #ff4b2b;
  border: 1px solid #ff4b2b;
}
.chima_title {
  margin-right: 10px;
}
</style>